<?php
namespace app\serverrecommend\service\recommend;

use \app\manage\service\ApiBase;

/**
 * @author:      李颖
 * @DateTime:    2017-09-28 17:29:08
 * @Description: 推荐位分类
 */
class Recommend extends ApiBase
{
    protected $recommendLogic = '\app\serverrecommend\logic\Recommend';
    protected $recommendValidate = '\app\serverrecommend\validate\Recommend';
    

    use \app\servergoods\traits\BaseCheckCate;

    //添加或编辑推荐位分类的时候进行检验分类
    private function _updateRecommend(array $data, bool $isEdit): array 
    {
        $checkData = $this->_baseCheckCate($data, '', 'edit', $this->recommendValidate, $this->recommendLogic);
        if($checkData['status'] != SUCCESS_STATUS){
            return $checkData;
        }
        if(isset($data['id'])){
            $res = $this->logic($this->recommendLogic)->editRecommend($data);
        }else{
            $data['level'] = $data['pid'] ? $checkData['res']['level'] + 1 : 0;
            $res = $this->logic($this->recommendLogic)->addRecommend($data);
        }
        return $res;
    }
    
    //添加推荐位分类
    public function addRecommend(): array 
    {
        $data = $this->haveIssetReceiveData(['app_id'=>0,'other_app_read'=>'','is_public'=>0,'pid'=>0,'name'=>' ','action'=>' ','profile_pic'=>'','desc'=>'','order'=>100]);
        return $this->_updateRecommend($data, false);
    }

    //编辑推荐位分类
    public function editRecommend(): array 
    {
        $data = $this->issetReceiveData(['id'=>0,'app_id'=>0,'other_app_read'=>'','is_public'=>0,'pid'=>0,'name'=>' ','action'=>' ','profile_pic'=>'','desc'=>'','order'=>100]);
        return $this->_updateRecommend($data, true);
    }

    // 获取本应用下的推荐位分类树
    public function getTree(): array 
    {
        $data = $this->haveIssetReceiveData(['id'=>0,'app_id'=>0]);
        return $this->stepRun($data,'getTree','getTree');
    }

    /*获取某个不删除的分类详情*/
    public function oneCateInfo(): array 
    {
        $data = $this->receiveData(['id'=>0,'app_id'=>0]);
        return $this->stepRun($data, 'oneCateInfo');
    }

    /*后台删除某个推荐分类*/
    public function delOneCate(): array 
    {
        $data = $this->receiveData(['id'=>0,'app_id'=>0]);
        return $this->stepRun($data, 'delOneCate');
    }

    /*前台获取action所属分类下的子类 多级*/
    public function getChildByAction(): array 
    {
        $data = $this->haveIssetReceiveData(['app_id'=>0, 'action'=>' ']);
        return $this->stepRun($data, 'getChildByAction');
    }

    /*前台获取action所属分类下的子类 一级*/
    public function getOneLevChildByAction(): array 
    {
        $data = $this->haveIssetReceiveData(['app_id'=>0, 'action'=>' ', 'limit'=>0]);
        return $this->stepRun($data, 'getOneLevChildByAction');
    }
    
}
